#include "ServerControlWidget.h"
#include "ServerConsole.h"

#include <QtWidgets/QPushButton>
#include <QtWidgets/QApplication>
#include <QtWidgets/QVBoxLayout>

int const ServerControlWidget::EXIT_CODE_REBOOT = -123456789;

ServerControlWidget::ServerControlWidget(QWidget *parent)
    : QWidget(parent)
{
    setWindowTitle(QString("Rsgis Server"));
    resize(800, 600);

    QPushButton *pButton = new QPushButton(this);
    pButton->setText(QString("Reboot"));
    //pButton->setFixedSize(75, 22);

    console = new ServerConsole;

    QVBoxLayout *pLayout = new QVBoxLayout();
    pLayout->addStretch();
    pLayout->addWidget(console);
//    pLayout->addStretch();

    pLayout->addWidget(pButton, 1, Qt::AlignCenter);
//    pLayout->addStretch();
    pLayout->setSpacing(10);
    pLayout->setMargin(10);

    setLayout(pLayout);

    connect(pButton, SIGNAL(clicked(bool)), this, SLOT(reboot()));
}

ServerControlWidget::~ServerControlWidget()
{
}

int ServerControlWidget::setMessage(QString s)
{
    if(console->blockCount()>800) console->setPlainText("");

    console->putData(s);
    return 1;
}

void ServerControlWidget::reboot()
{
    qApp->exit(ServerControlWidget::EXIT_CODE_REBOOT);
}

